package com.ziyun.erp.web.modules.store.storeGoodsProduct.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.store.storeGoodsProduct.entity.StoreGoodsProductEntity;
import com.ziyun.erp.modules.store.storeGoodsProduct.service.StoreGoodsProductService;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 门店自营产品表
 *
 * @author xueqz
 * @email 
 * @date 2018-09-25 16:10:54
 */
@RestController
@RequestMapping("storeGoodsProduct/storegoodsproduct")
public class StoreGoodsProductController {
    @Autowired
    private StoreGoodsProductService storeGoodsProductService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("storeGoodsProduct:storegoodsproduct:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = storeGoodsProductService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{productId}")
    @RequiresPermissions("storeGoodsProduct:storegoodsproduct:info")
    public R info(@PathVariable("productId") Integer productId){
        StoreGoodsProductEntity storeGoodsProduct = storeGoodsProductService.selectById(productId);

        return R.ok().put("storeGoodsProduct", storeGoodsProduct);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("storeGoodsProduct:storegoodsproduct:save")
    public R save(@RequestBody List<StoreGoodsProductEntity> storeGoodsProductList){
        if (storeGoodsProductList == null || storeGoodsProductList.size() == 0){
            return R.error();
        }
        String username = UserUtils.getCurentUser().getUsername();
        Long userId = UserUtils.getCurentUser().getUserId();
        for(int i = 0;i<storeGoodsProductList.size();i++){
            storeGoodsProductList.get(i).setCreateName(username);
            storeGoodsProductList.get(i).setProductNo(builderGoodsBn(storeGoodsProductList.get(i).getStoreCategoryId()));
//            storeGoodsProductList.get(i).setSellerId(userId);
        }
        storeGoodsProductService.insertBatch(storeGoodsProductList);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("storeGoodsProduct:storegoodsproduct:update")
    public R update(@RequestBody List<StoreGoodsProductEntity> storeGoodsProductList){
//        ValidatorUtils.validateEntity(storeGoodsProduct);
        //存入更新人
        if(storeGoodsProductList != null && storeGoodsProductList.size() > 0){
            storeGoodsProductService.updateAllColumnBatchById(storeGoodsProductList);
        }
//        storeGoodsProductService.updateAllColumnById(storeGoodsProduct);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("storeGoodsProduct:storegoodsproduct:delete")
    public R delete(@RequestBody Integer[] productIds){
//        storeGoodsProductService.deleteBatchIds(Arrays.asList(productIds));
        storeGoodsProductService.flagDelete(productIds);

        return R.ok();
    }

    /**
     * 生成商品编号
     */
    private String builderGoodsBn(Integer categoryId) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(sdf.format(date));
        stringBuffer.append(String.format("%6d", categoryId).replace(" ", "0"));
        stringBuffer.append(date.getTime());
        stringBuffer.append(String.format("%02d", random.nextInt(99)));
        return stringBuffer.toString();
    }

}
